<template>
	<view>
		<view style="padding: 20upx;font-size: 30upx;">

			<!-- 地址 -->
			<view class='address' @click="checkedAdd">
				<view v-if="address.length === 0" style="text-align: center; font-size: 30upx;color: #999;line-height: 180upx;">请选择收件地址</view>
				<view v-else>
					<view style="display: flex;justify-content: space-between;">
						<p>{{address.userName}}</p>
						<p>{{address.telNumber}}</p>
					</view>
					<view>{{address.provinceName}}{{address.cityName}}{{address.countyName}}{{address.detailInfo}}</view>
					<view v-if="address.isDefault === 1" style="text-align: right;color: #D7000F;">默认地址</view>
				</view>
			</view>

			<!-- 订单 -->
			<view v-for="(item,index) in order" :key='index'>
				<!-- <p>订单编号：</p> -->
				<view class="order-item">
					<image style="width: 160upx;height: 160upx;" :src="item.url"></image>
					<view style="margin-left: 20upx;width: 80%;">
						<p>{{item.name}}</p>
						<p class='spec'>{{item.spes_desc}}</p>
						<p style='display:flex;justify-content: space-between;'>
							<view>单价：¥ <text style="color: #D7000F;">{{item.price}}</text></view>
							<view>数量：{{item.num}}</view>
						</p>
					</view>
				</view>
				<view style="text-align: right;margin-top: 10upx;">
					<text style="line-height: 50upx;">共{{item.num}}件商品</text>
					<text style="margin-left: 20upx;">小计：{{item.price * item.num}}（元）</text>
				</view>
			</view>

			<view style="background: #FFFFFF;padding: 20upx;border-radius: 10upx;margin-top: 20upx;">
				<p>留言：</p>
				<textarea class="message" :max="200" v-model="memo"></textarea>
			</view>
		</view>
		<view style="position: fixed;bottom: 0;font-size: 30upx;display: flex;width: 100%;height: 80upx;line-height: 80upx;background: #FFFFFF;">
			<view style="flex: 1;text-align: right;padding-right: 30upx;">合计：<text style="color: #D7000F;font-size: 40upx;">{{goodsAmount}}</text>元</view>
			<button style="background: #D7000F;color: #FFFFFF;width: 300upx;font-size: 30upx" @click="confirmOrder()">提交订单</button>
		</view>
	</view>
</template>

<script>
	import {
		get,
		post,
		showToast
	} from '../../js/common';
	export default {
		data() {
			return {
				userId: uni.getStorageSync('userId'),
				productId: '',
				cartIds: [],
				order: [],
				address: [],
				memo: '',
				addId: '',
				goodsAmount: 0,
				num:0
			}
		},
		onLoad(op) {
			this.productId = op.productId ? op.productId : ''
			this.num = op.num? op.num : ''
			this.cartIds = op.cartId ? op.cartId : ''
			this.goodsAmount = op.total ? op.total : ''
		},
		onShow() {
			this.addId = uni.getStorageSync('checkAdd')
			this.getDetail()
		},
		methods: {
			getDetail() {
				if (this.productId) {
					this.order = []
					let params = {
						userId: this.userId,
						productId: this.productId
					}
					post('mobileapi/mobile/order/getConfirmOrderInfo', params).then(res => {
						this.order.push(res.orderInfo)
						this.order[0].num = this.num
						this.goodsAmount = this.order[0].price * this.num
						let addressList = this.order[0].addressList
						this.changeAdd(addressList)
					})
				} else {
					let params = {
						userId: this.userId,
						ids: this.cartIds
					}
					post('mobileapi/cart/getSettlementDetail', params).then(res => {
						this.order = res.data.productList
						let addressList = res.data.addressList
						this.changeAdd(addressList)
					})
				}
			},

			//获取默认地址
			setDefaultAdd(list) {
				for (let i = 0; i < list.length; i++) {
					if (list[i].isDefault === 1) {
						this.address = list[i]
						this.addId = list[i].id
						break
					} else {
						this.address = []
					}
				}
			},
			// 改变地址
			changeAdd(addressList) {
				if (this.addId === '') {
					this.setDefaultAdd(addressList)
				} else {
					for (let i = 0; i < addressList.length; i++) {
						if (this.addId === addressList[i].id) {
							this.address = addressList[i]
							// uni.setStorageSync('checkAdd', '')
							// this.addId = ''
							break
						}
					}
				}
			},
			// 选择地址
			checkedAdd() {
				uni.navigateTo({
					url: '../address/index?choose=' + true
				})
			},
			// 提交订单
			confirmOrder() {
				if (this.addId === '') {
					uni.showToast({
						title: '请选择收件地址',
                        icon:'none'
					})
				}
				if (this.productId) {
					let params = {
						goodsId: this.cartIds,
						productId: this.productId,
						addressId: this.addId,
						goodsAmount: this.goodsAmount,
						nums: this.num,
						userId: this.userId,
						memo: this.memo,
						goodsId: this.order[0].goodsId
					}
					post('mobileapi/mobile/order/submitOrder',params).then(res =>{
						uni.redirectTo({
							url:'../pay/index?orderId=' + res.data.orderId + '&total=' + res.data.goodsAmount + '&goodsName=' + res.data.goodsName
						})
					})
				} else {
					let params = {
						ids: this.cartIds,
						addressId: this.addId,
						goodsAmount: this.goodsAmount,
						userId: this.userId,
						memo: this.memo
					}
					post('mobileapi/cart/submitCartOrder',params).then(res => {
						uni.reLaunch({
							url:'../pay/index?orderId=' + res.data.orderId + '&total=' + res.data.goodsAmount
						})
					})
				}
			}
		},
	}
</script>
<style>
	.address {
		background: #FFFFFF;
		border-radius: 10upx;
		padding: 20upx;
		line-height: 50upx;
	}

	.order-item {
		margin-top: 20upx;
		background: #FFFFFF;
		padding: 20upx;
		border-radius: 10upx;
		display: flex;
	}

	.message {
		width: 97%;
		height: 100upx;
		margin-top: 20upx;
		border: 1px solid #999;
		border-radius: 10upx;
		padding: 10upx;
	}

	.spec {
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		line-height: 50upx;
		color: #666;
		font-size: 28upx;
	}
</style>
